<?php
/**
 * Created by PhpStorm.
 * User: jiang
 * Date: 2019/6/1
 * Time: 8:49
 */

namespace app\api\controller;
use app\api\model\AddressModel;

class Address extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        $this->address = new AddressModel();
    }
    public function AjaxReturn($code,$msg,$data=array())
    {

        $returnData=array(
            "code"=>$code
        );
        if($msg)
        {
            $returnData["message"]=$msg;
        }
        if(!empty($data))
        {
            $returnData["data"]=$data;
        }
        exit(json_encode($returnData));
    }
	//申请开店店铺类型下拉菜单显示
	//返回:类型name id  
    public function addresslist(){
        
        $list=$this->address->address();
        if(!empty($list)){
            return $this->AjaxReturn('1','查询成功',$list);
        }else{
            return $this->AjaxReturn('-1','暂无数据');
        }
    }
	//申请开店添加
	//传值:用户uid 店铺类型type (类型的id)店铺名称name 所在地区address
	//返回:成功 失败
    public function addressadd(){
        $uid=$this->uid;
		$count=$this->address->oneid($uid);
		if($count>0){
			return $this->AjaxReturn('-2','您已申请过店铺请耐心等待审核');
		}
        $name=input('name');
        $type=input('type');
        $address=input('address');
        $list=$this->address->addressadd($uid,$name,$address,$type);

        if(!empty($list)){
            return $this->AjaxReturn('1','添加成功');
        }else{
            return $this->AjaxReturn('-1','添加失败');
        }
    }
	//收货地址删除
	//传值:地址的id 
    public function areadel(){//地址删除
        $id=input('id');
        $res=$this->address->areadel($id);
        if(!empty($res)){
            return $this->AjaxReturn('1','添加成功');
        }else{
            return $this->AjaxReturn('-1','添加失败');
        }
    }
    /*public function updatedisplay(){//地址信息修改的显示
        $id=input('id');
        $res=$this->address->updatedisplay($id);
        if(!empty($res)){
            return $this->AjaxReturn('1','查询成功',$res);
        }else{
            return $this->AjaxReturn('1','查询失败');
        }
    }*/
	//收货地址添加
	//传值: 收货人姓名name 联系电话phone所在地区 area详细地址address
	//返回:成功 失败
	 public function areaadd(){
        $id=$this->uid;
        $name=input('name');
        $flg=input('flg');
        $phone=input('phone');
        $area=input('area');
        $address=input('address');
        $list=$this->address->areaadd($id,$name,$phone,$area,$address,$flg);
        if(!empty($list)){
            return $this->AjaxReturn('1','添加成功');
        }else{
            return $this->AjaxReturn('-1','添加失败');
        }
    }
	//收货地址修改
	//传值: 地址id 收货人姓名name 联系电话phone所在地区 area详细地址address
	//返回:成功 失败
	 public function areaupdate(){
        $uid=$this->uid;
		$id=input('id');
        $name=input('name');
        $phone=input('phone');
        $area=input('area');
        $address=input('address');
        $list=$this->address->areaupdate($id,$name,$phone,$area,$address);
        if(!empty($list)){
            return $this->AjaxReturn('1','修改成功');
        }else{
            return $this->AjaxReturn('-1','修改失败');
        }
    }
	//收货地址显示
	//返回:地址id收货人姓名name联系电话phone所在地区area详细地址address 用户id uid 默认地址(0普通地址1默认地址)
    public function areaindex(){
        $uid=$this->uid;
        $list=$this->address->areaindex($uid);
        if(!empty($list)){
            return $this->AjaxReturn('1','成功',$list);
        }else{
            return $this->AjaxReturn('-1','暂无数据');
        }
    }
	//修改默认收货地址
	//传值: 地址id
    public function areaupdates(){
        $id=input('id');
		$uid=$this->uid;
        $res=$this->address->areaupdates($id,$uid);
        if(!empty($res)){
            return $this->AjaxReturn('1','修改成功');
        }else{
            return $this->AjaxReturn('-1','修改失败');
        }
    }
}